<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dttx123.gitee.io","root":"/blog/","scheme":"Gemini","version":"8.0.0-rc.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="什么是WebApiAPI（Application Programming Interface,应用程序编程接口）,API是一些预先定义的方法，这些方法能够实现某些特定的功能，我们无须知道这些API的实现方式，但是我们需要知道这些API如何使用。  任何开发语言都会提供自己的API API的特征输入和输出(参数&#x2F;返回值)  通俗的讲，API就是编程语言给我提供的一些工具，通过这些工具，我们可以非常轻">
<meta property="og:type" content="article">
<meta property="og:title" content="webAPI 01">
<meta property="og:url" content="http://dttx123.gitee.io/2016/06/13/webAPI/01/index.html">
<meta property="og:site_name" content="Starry Night">
<meta property="og:description" content="什么是WebApiAPI（Application Programming Interface,应用程序编程接口）,API是一些预先定义的方法，这些方法能够实现某些特定的功能，我们无须知道这些API的实现方式，但是我们需要知道这些API如何使用。  任何开发语言都会提供自己的API API的特征输入和输出(参数&#x2F;返回值)  通俗的讲，API就是编程语言给我提供的一些工具，通过这些工具，我们可以非常轻">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2016/06/13/webAPI/01/javascript.png">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2016/06/13/webAPI/01/1.png">
<meta property="article:published_time" content="2016-06-12T16:00:00.000Z">
<meta property="article:modified_time" content="2020-05-13T03:24:55.442Z">
<meta property="article:author" content="大春哥">
<meta property="article:tag" content="dom操作">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://dttx123.gitee.io/blog/2016/06/13/webAPI/01/javascript.png">

<link rel="canonical" href="http://dttx123.gitee.io/2016/06/13/webAPI/01/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>webAPI 01 | Starry Night</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line toggle-line-first"></span>
        <span class="toggle-line toggle-line-middle"></span>
        <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Starry Night</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">这就朕为你打下的江山</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dttx123.gitee.io/2016/06/13/webAPI/01/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="大春哥">
      <meta itemprop="description" content="IT">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Starry Night">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          webAPI 01
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2016-06-13 00:00:00" itemprop="dateCreated datePublished" datetime="2016-06-13T00:00:00+08:00">2016-06-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-13 11:24:55" itemprop="dateModified" datetime="2020-05-13T11:24:55+08:00">2020-05-13</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="什么是WebApi"><a href="#什么是WebApi" class="headerlink" title="什么是WebApi"></a>什么是WebApi</h2><p>API（Application Programming Interface,应用程序编程接口）,API是一些预先定义的方法，这些方法能够实现某些特定的功能，我们无须知道这些API的实现方式，但是我们需要知道这些API如何使用。</p>
<ul>
<li>任何开发语言都会提供自己的API</li>
<li>API的特征输入和输出(参数/返回值)</li>
</ul>
<p>通俗的讲，API就是编程语言给我提供的一些工具，通过这些工具，我们可以非常轻易的完成一些功能。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">做饭需要一套做饭的工具：锅碗瓢盆</span><br><span class="line">打仗需要一套打仗的工具：刀枪剑戟</span><br><span class="line">找对象需一套找对象的工具：钱权颜缘</span><br><span class="line"></span><br><span class="line">Web API: 浏览器提供的一套操作网页（web）的API（方法），通过这套API我们可以非常轻易的操作页面的元素和浏览器的一些功能。</span><br></pre></td></tr></table></figure>

<p><strong>思考：我们学习Web API，我们该学什么？</strong> </p>
<p><img src="/blog/2016/06/13/webAPI/01/javascript.png" alt></p>
<p><strong>ECMAScript - JavaScript的核心</strong></p>
<p>定义了JavaScript 的语法规范</p>
<p>JavaScript的核心，描述了语言的基本语法和数据类型，ECMAScript是一套标准，定义了一种语言的标准。与具体实现无关</p>
<p><strong>BOM - 浏览器对象模型</strong></p>
<p>一套操作浏览器功能的API</p>
<p>通过BOM可以操作浏览器窗口，比如：弹出框、控制浏览器跳转、获取分辨率等 </p>
<p><strong>DOM - 文档对象模型</strong></p>
<p>一套操作页面元素的API</p>
<p>DOM可以把HTML看做是文档树，通过DOM提供的API可以对树上的节点进行操作</p>
<h1 id="DOM-文档对象模型"><a href="#DOM-文档对象模型" class="headerlink" title="DOM-文档对象模型"></a>DOM-文档对象模型</h1><h2 id="DOM基本概念"><a href="#DOM基本概念" class="headerlink" title="DOM基本概念"></a>DOM基本概念</h2><blockquote>
<p>DOM（Document Object Model）文档对象模型，是<code>W3C组织</code>推荐的一套用于处理<code>HTML</code>的标准<code>编程接口</code>。</p>
</blockquote>
<p>DOM又称为文档树模型，因为整个HTML文档是一个树形的结构</p>
<p><img src="/blog/2016/06/13/webAPI/01/1.png" alt></p>
<p>DOM中常见的概念</p>
<ul>
<li>文档<code>document</code>：一个网页可以称为文档</li>
<li>节点<code>node</code>：网页中的所有内容都是节点（标签、属性、文本、注释等）</li>
<li>元素<code>element</code>：网页中的标签</li>
</ul>
<h2 id="DOM初体验"><a href="#DOM初体验" class="headerlink" title="DOM初体验"></a>DOM初体验</h2><blockquote>
<p><code>document.getElementById(&quot;box&quot;)</code>的作用是根据页面中元素的id来获取元素</p>
</blockquote>
<p>在DOM中，想要操作一个元素，首先需要先获取到这个元素才能进行操作。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//功能：通过id获取元素</span></span><br><span class="line"><span class="comment">//参数：字符串类型的id值</span></span><br><span class="line"><span class="comment">//返回值：元素，如果id不存在，会返回null</span></span><br><span class="line"><span class="keyword">var</span> div = <span class="built_in">document</span>.getElementById(<span class="string">'main'</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.dir(div);</span><br></pre></td></tr></table></figure>

<p>关于<code>console.log</code>和<code>console.dir</code>的区别</p>
<ul>
<li><code>console.log</code>打印一个元素的时候，是以标签的形式进行展示的</li>
<li><code>console.dir</code>打印一个元素的时候，是以对象的形式进行展示的</li>
</ul>
<p>在DOM中，页面标签的属性和DOM对象的属性是一一对应的，因此我们可以通过修改DOM对象的属性来修改标签的属性。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">//html内容</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">title</span>=<span class="string">"hehe"</span> <span class="attr">id</span>=<span class="string">"box"</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="actionscript"><span class="comment">// javascript代码</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">var</span> div = <span class="built_in">document</span>.getElementById(<span class="string">'box'</span>);</span></span><br><span class="line"><span class="actionscript">div.title = <span class="string">"哈哈"</span>;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line">//结果：div标签的title属性就发生了更改</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">title</span>=<span class="string">"哈哈"</span> <span class="attr">id</span>=<span class="string">"box"</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h1 id="获取元素"><a href="#获取元素" class="headerlink" title="获取元素"></a>获取元素</h1><h2 id="使用getElementById的注意事项"><a href="#使用getElementById的注意事项" class="headerlink" title="使用getElementById的注意事项"></a>使用getElementById的注意事项</h2><ul>
<li>如果id不存在，会返回null</li>
<li>在DOM中，<code>document.getElementById(&quot;box&quot;)</code>方法需要写在html内容的后面，保证页面加载完成之后才能获取到内容。</li>
<li>部分浏览器，如果元素设置了id属性，可以直接使用（但是不是规范，不推荐）</li>
</ul>
<p><strong>当getElementById返回null的时候，null.onclick就会报错</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--没有id--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">"images/1.jpg"</span> <span class="attr">alt</span>=<span class="string">"图破了"</span> <span class="attr">title</span>=<span class="string">"呵呵"</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="actionscript">  <span class="comment">//如果找不到，返回的是null，试图给null设置属性会报错</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">var</span> element = <span class="built_in">document</span>.getElementById(<span class="string">"img"</span>);</span></span><br><span class="line"><span class="actionscript">  element.title = <span class="string">"嘿嘿"</span>;<span class="comment">//报错Uncaught TypeError: Cannot set property 'title' of null</span></span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Document/getElementById" target="_blank" rel="noopener">mdn地址</a></p>
<h2 id="getElementsByTagName"><a href="#getElementsByTagName" class="headerlink" title="getElementsByTagName"></a>getElementsByTagName</h2><blockquote>
<p>通过标签名获取元素</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//参数：标签名</span></span><br><span class="line"><span class="comment">//返回值：一个伪数组， 伪数组不是数组，不能使用数组的方法，但是可以跟数组一样进行遍历和使用下标进行操作。</span></span><br><span class="line"><span class="keyword">var</span> divs = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'div'</span>);</span><br></pre></td></tr></table></figure>

<p><strong>注意：返回值有没有获取到元素，都是一个伪数组，即便元素只有一个</strong></p>
<h2 id="缩小查找范围"><a href="#缩小查找范围" class="headerlink" title="缩小查找范围"></a>缩小查找范围</h2><ol>
<li><code>document.getElemntsByTagName(&quot;div&quot;)</code>表示获取页面中所有的div元素，范围太大了，可以使用<code>box.getElementsByTagName(&quot;div&quot;)</code>表示获取box中所有的div元素，范围更小。</li>
<li><code>document.getElementById(&quot;demo&quot;)</code>表示获取页面中id为box的元素，没有<code>box.getElementById(&quot;demo&quot;)</code>这种用法。</li>
</ol>
<h1 id="注册事件"><a href="#注册事件" class="headerlink" title="注册事件"></a>注册事件</h1><h2 id="事件的基本使用"><a href="#事件的基本使用" class="headerlink" title="事件的基本使用"></a>事件的基本使用</h2><blockquote>
<p>事件：触发-响应机制</p>
</blockquote>
<p>事件三要素：</p>
<ul>
<li>事件源:触发事件的元素</li>
<li>事件名称: click 点击事件</li>
<li>事件处理程序:事件触发后要执行的代码(函数形式)</li>
</ul>
<p>注册事件的基本语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> box = <span class="built_in">document</span>.getElementById(<span class="string">'box'</span>);</span><br><span class="line"><span class="comment">//on:当  click:点击   当按钮被点击的时候触发</span></span><br><span class="line">box.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'代码会在box被点击后执行'</span>);  </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><strong>注意：事件处理程序并不是立马执行，而是当事件触发的时候在会执行（浏览器会自动调用）</strong></p>
<p>练习：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">点击按钮，切换图片</span><br></pre></td></tr></table></figure>

<h2 id="修改标签的属性"><a href="#修改标签的属性" class="headerlink" title="修改标签的属性"></a>修改标签的属性</h2><blockquote>
<p>在DOM中，页面标签的属性和DOM对象的属性是一一对应的，因此我们可以通过修改DOM对象的属性来修改标签的属性。</p>
</blockquote>
<p>标签的alt，title，src，width，height等属性，可以直接通过对象进行修改。</p>
<p><strong>如果是class属性，在js中class是关键字，因此对应的是className属性</strong></p>
<p>练习：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. 点击按钮，修改div的样式 -- 定义类名</span></span><br><span class="line"><span class="comment">//2. 点击按钮，div进行显示和隐藏（两个按钮） -- 预定义类名</span></span><br><span class="line"><span class="comment">//3. 点击按钮，div进行显示和隐藏（一个按钮） -- 预定义类名</span></span><br></pre></td></tr></table></figure>



<p>【案例：二维码案例】</p>
<p>鼠标经过事件与鼠标离开事件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">onmouseover:当鼠标经过时触发</span><br><span class="line">onmouseout:当鼠标离开时触发</span><br></pre></td></tr></table></figure>



<h2 id="事件中的this"><a href="#事件中的this" class="headerlink" title="事件中的this"></a>事件中的this</h2><blockquote>
<p>当在事件中表示当前元素的时候，可以使用this</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> btn = <span class="built_in">document</span>.getElementById(<span class="string">"btn"</span>);</span><br><span class="line">btn.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">//给 btn注册的事件，因此this表示btn</span></span><br><span class="line">  <span class="keyword">this</span>.value = <span class="string">"哈哈"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>练习：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. 给多个按钮注册点击事件，点击的那个变成红色</span></span><br><span class="line"><span class="comment">//2. 给多个按钮注册点击事件，当点击按钮，显示对应的图片 -- title属性记录图片src</span></span><br></pre></td></tr></table></figure>



<h2 id="阻止a标签跳转"><a href="#阻止a标签跳转" class="headerlink" title="阻止a标签跳转"></a>阻止a标签跳转</h2><blockquote>
<p>对于a标签来说，默认的行为就是进行页面跳转，如果不想让a标签进行跳转，可以在注册事件中使用<code>return false</code></p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> link = <span class="built_in">document</span>.getElementById(<span class="string">"link"</span>);</span><br><span class="line">link.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  alert(<span class="string">"呵呵"</span>);</span><br><span class="line">  <span class="comment">//阻止页面跳转</span></span><br><span class="line">  <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="标签的内容属性"><a href="#标签的内容属性" class="headerlink" title="标签的内容属性"></a>标签的内容属性</h1><blockquote>
<p>innerText &amp;&amp; innerHTML</p>
</blockquote>
<h2 id="美女相册案例"><a href="#美女相册案例" class="headerlink" title="美女相册案例"></a>美女相册案例</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. 找对象</span></span><br><span class="line"><span class="keyword">var</span> imgs = <span class="built_in">document</span>.getElementById(<span class="string">"imgs"</span>);<span class="comment">//ul</span></span><br><span class="line"><span class="keyword">var</span> links = imgs.getElementsByTagName(<span class="string">"a"</span>);<span class="comment">//所有的a标签</span></span><br><span class="line"><span class="keyword">var</span> bigImg = <span class="built_in">document</span>.getElementById(<span class="string">"bigImg"</span>);<span class="comment">//大图片</span></span><br><span class="line"><span class="keyword">var</span> des = <span class="built_in">document</span>.getElementById(<span class="string">"des"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//2. 给所有的a标签注册点击事件</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; links.length; i++) &#123;</span><br><span class="line">  links[i].onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//3. 修改大图片的src属性， this.href</span></span><br><span class="line">    bigImg.src = <span class="keyword">this</span>.href;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//4. 修改p标签的内容, innerText:内部的文本</span></span><br><span class="line">    des.innerText = <span class="keyword">this</span>.title;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//5. 阻止a跳转</span></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/dom%E6%93%8D%E4%BD%9C/" rel="tag"># dom操作</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2015/01/01/hexo/hexo%E9%85%8D%E7%BD%AE/" rel="prev" title="hexo">
      <i class="fa fa-chevron-left"></i> hexo
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2016/07/03/webAPI/02/" rel="next" title="webAPI 02">
      webAPI 02 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是WebApi"><span class="nav-text">什么是WebApi</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DOM-文档对象模型"><span class="nav-text">DOM-文档对象模型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#DOM基本概念"><span class="nav-text">DOM基本概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DOM初体验"><span class="nav-text">DOM初体验</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#获取元素"><span class="nav-text">获取元素</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#使用getElementById的注意事项"><span class="nav-text">使用getElementById的注意事项</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#getElementsByTagName"><span class="nav-text">getElementsByTagName</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#缩小查找范围"><span class="nav-text">缩小查找范围</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#注册事件"><span class="nav-text">注册事件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#事件的基本使用"><span class="nav-text">事件的基本使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改标签的属性"><span class="nav-text">修改标签的属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件中的this"><span class="nav-text">事件中的this</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#阻止a标签跳转"><span class="nav-text">阻止a标签跳转</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#标签的内容属性"><span class="nav-text">标签的内容属性</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#美女相册案例"><span class="nav-text">美女相册案例</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="大春哥"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">大春哥</p>
  <div class="site-description" itemprop="description">IT</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">35</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">大春哥</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

</body>
</html>
